package com.yoomiito.app.ui.anyview.gasstation

import android.content.Context
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.R
import com.yoomiito.app.model.oil.OilGunNum
import com.yoomiito.app.model.oil.OilNum
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.widget.SpacingDecoration

class GasStationFormatView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
    private val adapter1 = OilNumAdapter(null)
    private val adapter2 = OilGunAdapter(null)
    private var mSelectOilGunNum = -1 //选中的枪号
    private var mSelectOilNum: OilNum? = null //选中的油号
    var onSelectListener: (OilNum?, Int) -> Unit = { _, _ -> }

    init {
        val l = DensityUtil.dp2px(15f)
        val t = DensityUtil.dp2px(20f)
        setPadding(l, t, l, t)
        orientation = VERTICAL
        val subtitleTv1 = TextView(context)
        subtitleTv1.paint.isFakeBoldText = true
        subtitleTv1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 13f)
        subtitleTv1.text = "选择油号"
        val subtitle1Lp = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        subtitle1Lp.bottomMargin = DensityUtil.dp2px(12f)
        addView(subtitleTv1, subtitle1Lp)

        val rcy1 = androidx.recyclerview.widget.RecyclerView(context)
        rcy1.layoutManager = androidx.recyclerview.widget.GridLayoutManager(context, 4)
        val sp = DensityUtil.dp2px(10f)
        rcy1.addItemDecoration(SpacingDecoration(sp, sp, false))
        rcy1.adapter = adapter1
        adapter1.setOnItemClickListener { adapter, view, position ->
            val oilNum = adapter1.data[position]
            adapter1.data.forEachIndexed { index, oilNum ->
                if (index == position) {
                    oilNum.select = true
                    this.mSelectOilNum = oilNum
                } else {
                    oilNum.select = false
                }
            }
            adapter1.notifyDataSetChanged()
            //切换油号 枪号复原
            oilNum.oilGunNum.forEach {
                it.select = false
            }
            adapter2.setNewData(oilNum.oilGunNum)
            onSelectListener(mSelectOilNum, -1)
        }
        val rcy1Lp = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        addView(rcy1, rcy1Lp)

        val subtitleTv2 = TextView(context)
        subtitleTv2.paint.isFakeBoldText = true
        subtitleTv2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 13f)
        subtitleTv2.text = "选择枪号"
        val subtitleTv2Lp = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        subtitleTv2Lp.topMargin = DensityUtil.dp2px(20f)
        subtitleTv2Lp.bottomMargin = DensityUtil.dp2px(12f)
        addView(subtitleTv2, subtitleTv2Lp)

        val rcy2 = androidx.recyclerview.widget.RecyclerView(context)
        rcy2.layoutManager = androidx.recyclerview.widget.GridLayoutManager(context, 4)
        rcy2.addItemDecoration(SpacingDecoration(sp, sp, false))
        rcy2.adapter = adapter2
        adapter2.setOnItemClickListener { adapter, view, position ->
            adapter2.data.forEachIndexed { index, oilGunNum ->
                if (index == position) {
                    oilGunNum.select = true
                    mSelectOilGunNum = oilGunNum.num
                } else {
                    oilGunNum.select = false
                }
            }
            adapter2.notifyDataSetChanged()
            onSelectListener(mSelectOilNum, mSelectOilGunNum)
        }
        val rcy2Lp = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        addView(rcy2, rcy2Lp)
    }

    fun setOilNum(data: List<OilNum>) {
        adapter1.setNewData(data)
        var select = false
        data.forEach {
            if (it.select) {
                select = true
                mSelectOilNum = it
                adapter2.setNewData(it.oilGunNum)
            }
        }
        if (!select) adapter2.setNewData(null)
    }

    private inner class OilNumAdapter(data: List<OilNum>?) : BaseQuickAdapter<OilNum, BaseViewHolder>(R.layout.item_tv, data) {
        override fun convert(helper: BaseViewHolder?, item: OilNum?) {
            helper?.getView<TextView>(R.id.item_tv)?.let {
                it.text = "${item?.name}"
                it.background = if (item?.select!!) ResUtil.getDrawable(R.drawable.bg_color_3bb033_15) else ResUtil.getDrawable(R.drawable.bg_flowlayout)
            }
        }
    }

    private inner class OilGunAdapter(data: List<OilGunNum>?) : BaseQuickAdapter<OilGunNum, BaseViewHolder>(R.layout.item_tv, data) {
        override fun convert(helper: BaseViewHolder?, item: OilGunNum?) {
            helper?.getView<TextView>(R.id.item_tv)?.let {
                it.text = "${item?.num}"
                it.background = if (item?.select!!) ResUtil.getDrawable(R.drawable.bg_color_3bb033_15) else ResUtil.getDrawable(R.drawable.bg_flowlayout)
            }
        }
    }
}